package com.ruoyi.biz.controller;

import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.entity.LotteryUserDO;
import com.ruoyi.biz.entity.WechatUserDO;
import com.ruoyi.biz.service.LotteryUserService;
import com.ruoyi.biz.service.WechatUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/biz/lotteryUser")
public class LotteryUserController extends BaseController {

    private String prefix = "lotteryUser";

    @Resource
    LotteryUserService lotteryUserService;
    @Resource
    WechatUserService wechatUserService;

    @GetMapping()
    public String build() {
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LotteryUserDO lotteryUserDO) {
        startPage();
        List<LotteryUserDO> list = lotteryUserService.queryList(lotteryUserDO);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @Log(title = "抽奖用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LotteryUserDO lotteryUserDO) {
        if (ValidateUtil.isEmpty(lotteryUserDO)) {
            return error("入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDO.getTel())) {
            return error("联系电话不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDO.getPsnName())) {
            return error("姓名不能为空！");
        }
        LotteryUserDO qLotteryUserDO = new LotteryUserDO();
        qLotteryUserDO.setTel(lotteryUserDO.getTel());
        qLotteryUserDO.setPsnName(lotteryUserDO.getPsnName());
        qLotteryUserDO.setValiFlag("1");
        List<LotteryUserDO> list = lotteryUserService.queryList(qLotteryUserDO);
        if (!ValidateUtil.isEmpty(list)) {
            return error("已经存在过此人员！");
        }
        lotteryUserDO.setValiFlag("1");
        lotteryUserDO.setCrterId(getSysUser().getUserId().toString());
        lotteryUserDO.setCrterName(getSysUser().getUserName());
        lotteryUserDO.setCrteTime(new Date());
        lotteryUserDO.setOpterId(getSysUser().getUserId().toString());
        lotteryUserDO.setOpterName(getSysUser().getUserName());
        lotteryUserDO.setOptTime(new Date());
        return toAjax(lotteryUserService.insert(lotteryUserDO));
    }

    @GetMapping("/edit/{lotteryUserId}")
    public String edit(@PathVariable("lotteryUserId") String lotteryUserId, ModelMap mmap) {
        LotteryUserDO qLotteryUserDO = new LotteryUserDO();
        qLotteryUserDO.setLotteryUserId(lotteryUserId);
        qLotteryUserDO.setValiFlag("1");
        List<LotteryUserDO> list = lotteryUserService.queryList(qLotteryUserDO);
        mmap.put("lotteryUser", list.get(0));
        return prefix + "/edit";
    }

    @Log(title = "抽奖用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LotteryUserDO lotteryUserDO) {
        if (ValidateUtil.isEmpty(lotteryUserDO)) {
            return error("入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDO.getTel())) {
            return error("联系电话不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDO.getPsnName())) {
            return error("姓名不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDO.getLotteryUserId())) {
            return error("用户ID不能为空！");
        }

        LotteryUserDO qLotteryUserDO = new LotteryUserDO();
        qLotteryUserDO.setTel(lotteryUserDO.getTel());
        qLotteryUserDO.setPsnName(lotteryUserDO.getPsnName());
        qLotteryUserDO.setValiFlag("1");
        List<LotteryUserDO> list = lotteryUserService.queryList(qLotteryUserDO);
        if (!ValidateUtil.isEmpty(list)) {
            //排除自己
            list = list.stream().filter(item -> !lotteryUserDO.getLotteryUserId().equals(item.getLotteryUserId())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(list)) {
                return error("已存在此用户，不能修改！");
            }
        }
        lotteryUserDO.setOpterId(getSysUser().getUserId().toString());
        lotteryUserDO.setOpterName(getSysUser().getUserName());
        lotteryUserDO.setOptTime(new Date());
        return toAjax(lotteryUserService.updateLotteryUser(lotteryUserDO));
    }

    @Log(title = "抽奖用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lotteryUserService.deleteLotteryUser(ids));
    }

    @GetMapping("/wechatInfo/{userId}")
    public String wechatInfo(@PathVariable("userId") String userId, ModelMap mmap) {
        WechatUserDO qWechatUserDO = new WechatUserDO();
        qWechatUserDO.setUserId(userId);
        List<WechatUserDO> list = wechatUserService.queryList(qWechatUserDO);
        mmap.put("wechatUser", list.get(0));
        return prefix + "/wechatInfo";
    }

    /**
     * 用户解绑，目前解绑功能只是让手机号姓名和微信信息失去关联关系，暂时没有删除掉这个人的抽奖记录
     * @param lotteryUserId
     * @return
     */
    @Log(title = "抽奖用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/removeBind")
    @ResponseBody
    public AjaxResult removeBind(String lotteryUserId) {
        LotteryUserDO qLotteryUserDO = new LotteryUserDO();
        qLotteryUserDO.setLotteryUserId(lotteryUserId);
        qLotteryUserDO.setValiFlag("1");
        List<LotteryUserDO> lotteryUserDOS = lotteryUserService.queryList(qLotteryUserDO);
        if (ValidateUtil.isEmpty(lotteryUserDOS)) {
            return error("未查询到抽奖用户信息！");
        }
        if (ValidateUtil.isEmpty(lotteryUserDOS.get(0).getUserId())) {
            return error("此用户未关联微信信息！");
        }
        return toAjax(wechatUserService.removeBind(lotteryUserId, lotteryUserDOS.get(0).getUserId()));
    }
}
